//当前页面业务
var compare ;
function initWidgetView(_compare) {
    compare = _compare;
    // console.log(_compare);
    compareView.setLayerList();
}
//模块：
var compareView = {
    layersObj : {},  
    updateLayerVisible: function (cfg, visible) {
        if (!this.isActivate) return;
        var layersCfg = this.viewerEx.gisdata.config.operationallayers;
        if (layersCfg && layersCfg.length > 0) {
            for (var i = 0; i < layersCfg.length; i++) {
                var item = layersCfg[i];
                if (item.id == cfg.id && item.pid == cfg.pid && item.name == cfg.name) {
                    if (item._layer)
                        item._layer.setVisible(visible);
                    break;
                }
            }
        }
    },
    getLayers: function () {
        if (compare._layers == null) {
            var layers = [];
            var basemapsCfg = this.hasManagerBaseMaps ? compare.viewerEx.gisdata.config.basemaps : [];
            var operationallayersCfg = compare.viewerEx.gisdata.config.operationallayers;
            //记录所有id，方便计算nextid
            compareView._tempIdx = 1;
            compareView.arrIdx = [];
            for (var i = 0; i < basemapsCfg.length; i++) {
                var item = basemapsCfg[i];

                if (item.id)
                compareView.arrIdx.push(item.id);
            }
            for (var i = 0; i < operationallayersCfg.length; i++) {
                var item = operationallayersCfg[i];
                if (item.id)
                compareView.arrIdx.push(item.id);
            }
            //构建集合，预处理相关数据  
            for (var i = 0; i < basemapsCfg.length; i++) {
                var item = basemapsCfg[i];

                if (!item.name)
                    item.name = "未命名";
                if (!item.id)
                    item.id = this.getNextId();
                if (!item.pid)
                    item.pid = -1;
                layers.push(item);
                if (item.type == "group" && item.layers) {
                    for (var idx = 0; idx < item.layers.length; idx++) {
                        var childitem = item.layers[idx];
                        childitem.pid = item.id;
                        childitem.id = this.getNextId();
                        if (!childitem.name)
                            childitem.name = item.name + '-' + (idx + 1);

                        childitem._parent = item;
                        layers.push(childitem);
                    }
                }
            }
            for (var i = 0; i < operationallayersCfg.length; i++) {
                var item = operationallayersCfg[i];
                if (!item.name)
                    item.name = "未命名";
                if (!item.id)
                    item.id = this.getNextId();
                if (!item.pid)
                    item.pid = -1;
                layers.push(item);
                if (item.type == "group" && item.layers) {
                    for (var idx = 0; idx < item.layers.length; idx++) {
                        var childitem = item.layers[idx];
                        childitem.pid = item.id;
                        childitem.id = this.getNextId();
                        if (!childitem.name)
                            childitem.name = item.name + '-' + (idx + 1);

                        childitem._parent = item;
                        layers.push(childitem);
                    }
                }
            }
            //初始化顺序字段,
            for (var i = 0; i < layers.length; i++) {
                var item = layers[i];

                //计算层次顺序
                var order = Number(item.order);
                if (isNaN(order))
                    order = i;//(i + 1) ;
                item.order = order;

                //主键，用于存储取图层用，防止有重复
                item._key = i + "_" + item.id + "_" + item.name;

                //图层的处理
                if (item._layer != null) {
                    compareView.udpateLayerZIndex(item._layer, order);
                }
            }
            compare._layers = layers;
        }
        return compare._layers;
    },
    //更新图层:顺序
    udpateLayerZIndex: function (model, order) { 
        model.setZIndex(order);
    },
    //数据初始化处理
    _tempIdx: 1,
    arrIdx: [],
    setLayerList :function (){
        var that = this;
        //初始化树
        var setting = {
            check: {
                enable: true
            },
            data: {
                simpleData: {
                    enable: true
                }
            },
            callback: {
                onCheck: that.treeOverlays_onCheck,
                // onDblClick: that.treeOverlays_onDblClick
            },
            view: {
                // addDiyDom: addOpacityRangeDom
            }
        };
        var zNodes = [];
        var layers = compareView.getLayers();
        for (var i = layers.length - 1; i >= 0; i--) {
            var item = layers[i];

            var node = {
                id: item.id,
                pId: item.pid,
                name: item.name,
                _type: item.type,
                _key: item._key,
            };
            if (item._layer == null || item.type == "group") {
                node.icon = "";
                node.open = item.open == null ? true : item.open;
                zNodes.push(node);
            } else {
                node.icon = "";
                node.checked = false;
                if (item._parent)
                    node._parent = item._parent._key;
                //记录图层
                compareView.layersObj[node._key] = item._layer;
                zNodes.push(node);
            }
        }
        $.fn.zTree.init($("#treeOverlays_left"), setting, zNodes);
    },
    //没有id的图层，进行id赋值处理
    getNextId: function () {
        while (compareView.arrIdx.indexOf(compareView._tempIdx) != -1) {
            compareView._tempIdx++;
        }
        compareView.arrIdx.push(compareView._tempIdx);
        return compareView._tempIdx;
    },
    //更新图层:显示隐藏状态
    updateLayerVisible: function (model, visible) {
        model.setVisible(visible);
        // if (visible)
        //     model.centerAt();
    },
    //viewEx图层渲染
    treeOverlays_onCheck : function(e, treeId, treeNode){
        var that = this;
        var zTree = $.fn.zTree.getZTreeObj(treeId);
        //获得所有改变check状态的节点
        var changedNodes = zTree.getChangeCheckedNodes();
        for (var i = 0; i < changedNodes.length; i++) {
            var treeNode = changedNodes[i];
            treeNode.checkedOld = treeNode.checked;
            var layer = compareView.layersObj[treeNode._key];
            if (layer == null) continue;
            //显示隐藏透明度设置view
            if (treeNode.checked)
                $("#" + treeNode.tId + "_range").show();
            else
                $("#" + treeNode.tId + "_range").hide();
            //单选的互斥的节点，特殊处理下。 
            if (treeNode._type == "terrain" && treeNode.checked) { 
                function filter(node) {
                    return node._type == "terrain" && node._key != treeNode._key;
                }  
                var nodes = zTree.getNodesByFilter(filter, false, treeNode.getParentNode());
                for (var nidx = 0; nidx < nodes.length; nidx++) {

                    nodes[nidx].checkedOld = false;
                    zTree.checkNode(nodes[nidx], false, true);

                    var layertmp = compareView.layersObj[nodes[nidx]._key];
                    compareView.updateLayerVisible(layertmp, false);
                } 
            }
            //处理图层显示隐藏
            if (treeNode._parent) {
                var parentLayer = compareView.layersObj[treeNode._parent];
                compareView.updateLayerVisible(layer, treeNode.checked, parentLayer);
            } else {
                compareView.updateLayerVisible(layer, treeNode.checked);
            }
        }
    },
    treeOverlays_onDblClick : function (event, treeId, treeNode) {
        var layer = compareView.layersObj[treeNode._key];
        if (layer == null) return;
        model.centerAt();
    }
};